import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import axios from 'axios'
export default new Vuex.Store({
  state: {
    list: [],
    user: {
      isLogin: localStorage.getItem('token') ? true : false,
      name: localStorage.getItem('token')
    }
  },
  mutations: {
    // 初始化数据
    Fetch_List(state, payload) {
      state.list = payload
    },
    // 删除数据
    Delete_List(state, id) {
      state.list = state.list.filter(item => item.id !== id)
    },
    // 添加数据
    Add_List(state, payload) {
      state.list.push(payload)
    },
    // 修改数据
    Edit_List(state, payload) {
      let o = state.list.find(item => item.id === payload.id)
      // o.name = payload.name
      // o.desc = payload.desc
      // o.address = payload.address
      Object.assign(o, payload)
    },
    Set_User(state, payload) {
      state.user = payload
    }
  },
  getters: {
    searchList(state) {
      return searchValue => state.list.filter(item => item.name.includes(searchValue))
    }
  },
  actions: {
    // 获取数据
    Fetch_List({ commit }) {
      axios.get('/list').then(resp => {
        commit('Fetch_List', resp.data.data)
      })
    }
  },
  modules: {
  }
})
